package com.feib.soeasy.action;

import java.util.ArrayList;

import com.feib.soeasy.model.Group;
import com.feib.soeasy.model.Payment;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.PaymentService;
import com.feib.soeasy.util.UserUtil;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.ValidationErrorHandler;
import net.sourceforge.stripes.validation.ValidationErrors;

public class Soez2005ActionBean extends BaseActionBean implements ValidationErrorHandler {

    private static final String DEFAULT_JSP = "/WEB-INF/views/soez2005/soez2005.jsp";
    private static final String UPLOAD_LIST_JSP = "/WEB-INF/views/soez2005/soez2005List.jsp";
    private static final String PAYMENT_ITEM_JSP = "/WEB-INF/views/soez2005/paymentList.jsp";
    /**
     * 帳單放行結果畫面
     */
    private static final String AFTER_CONFIRM_JSP = "/WEB-INF/views/soez2005/afterConfirm.jsp";

    private Payment resultPayment;
    private PaginatedList<Payment> queryPaymentResult;
    private Payment qryPayment;

    private ArrayList<Payment> checkPayment ;

    @SpringBean("paymentService")
    private PaymentService paymentService;

    /**
     * 檔案放行結果
     */
    private String confirmMessage;

    @DontBind
    @DefaultHandler
    public Resolution doDefault()
    {
        logger.debug("Enter doDefault mehtod.");
        return new ForwardResolution(DEFAULT_JSP); 
    }


    public Resolution doAjaxQuery()
    {
        logger.debug("Enter doAjaxQuery mehtod.");
        try {
            if (null == qryPayment)
                qryPayment = new Payment();
            qryPayment.setPaymentStatus(2);


            // 查上傳人員與目前使用者同一個Group 的案件
            if (!UserUtil.isBankGroup())
            {
                Group group = new Group();
                group.setGroupPk(UserUtil.getUserGroup().getGroupPk());
                qryPayment.setGroup(group);
            }

            if (null == queryPaymentResult)
                queryPaymentResult = new PaginatedList<Payment>();
            
            queryPaymentResult.setPageSize(1000);

            //呼叫服務查詢
            queryPaymentResult.setPaginatedList(
                            paymentService.queryByPaymentStatus2( qryPayment, queryPaymentResult));

        }catch (Exception e) {
            logger.error("上傳記錄查詢錯誤(Soez2005ActionBean.doAjaxQuery):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQuery.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }
        return new ForwardResolution(UPLOAD_LIST_JSP);
    }

    public Resolution doAjaxQueryPayments()
    {
        if (null == qryPayment)
            qryPayment = new Payment();

        logger.debug("Enter doAjaxQueryPayments mehtod.");
        try {
            resultPayment = 
                paymentService.getPaymentByPk(qryPayment);
        }
        catch (Exception e) {
            logger.error("", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQueryPaymentsAction.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }
        return new ForwardResolution(PAYMENT_ITEM_JSP);
    }

    public Resolution doAjaxPaymentsConfirm()
    {
        logger.debug("Enter doAjaxPaymentsConfirm mehtod.");
        try {
            if (null == checkPayment)
                checkPayment = new ArrayList<Payment>();
            if (null == qryPayment)
                qryPayment = new Payment();
            
            paymentService.updateWriteOffPaymentStatus(checkPayment, qryPayment.getPaymentStatus()+"");
            
            if (!"2".equals(qryPayment.getPaymentStatus()+""))
                confirmMessage = "帳單核銷放行完成！";
            else
                confirmMessage = "帳單核銷退回！";

        } catch (Exception e) {
            confirmMessage = "處理失敗！";
            logger.error("", e);

        }
        return new ForwardResolution(AFTER_CONFIRM_JSP);
    }

    public ArrayList<Payment> getCheckPayment() {
        return checkPayment;
    }

    public void setCheckPayment(ArrayList<Payment> checkPayment) {
        this.checkPayment = checkPayment;
    }

    public Payment getQryPayment() {
        return qryPayment;
    }

    public void setQryPayment(Payment qryPayment) {
        this.qryPayment = qryPayment;
    }

    public Payment getResultPayment() {
        return resultPayment;
    }

    public void setResultPayment(Payment resultPayment) {
        this.resultPayment = resultPayment;
    }

    public PaginatedList<Payment> getQueryPaymentResult() {
        return queryPaymentResult;
    }

    public void setQueryPaymentResult(PaginatedList<Payment> queryPaymentResult) {
        this.queryPaymentResult = queryPaymentResult;
    }


    
    /**
     * @return the confirmMessage
     */
    public String getConfirmMessage() {
        return confirmMessage;
    }


    
    /**
     * @param confirmMessage the confirmMessage to set
     */
    public void setConfirmMessage(String confirmMessage) {
        this.confirmMessage = confirmMessage;
    }
}